package com.pig4cloud.pig.module.controller.material;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.MaterialGroup;
import com.pig4cloud.pig.module.service.base.IMaterialGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 素材分组Controller
 *
 * @author ruoyi
 * @date 2021-08-13
 */
@Validated
@Api(value = "素材分组控制器", tags = {"素材分组管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/material/group")
public class MaterialGroupController {

    private final IMaterialGroupService materialGroupService;

    /**
     * 查询素材分组列表
     */
    @ApiOperation("查询素材分组列表")
    @GetMapping("/list")
    public TableDataInfo<MaterialGroup> list(@Validated MaterialGroup materialGroup) {
        return PageUtils.buildDataInfo(materialGroupService.page(PageUtils.buildPage()));
    }


    /**
     * 查询素材分组列表
     */
    @ApiOperation("查询素材分组列表")
    @GetMapping("/listNoPage")
    public R listNoPage(@Validated MaterialGroup materialGroup) {
        return R.ok(materialGroupService.list());
    }

    /**
     * 获取素材分组详细信息
     */
    @ApiOperation("获取素材分组详细信息")
    @GetMapping("/{id}")
    public R<MaterialGroup> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") String id) {
        return R.ok(materialGroupService.getById(id));
    }

    /**
     * 新增素材分组
     */
    @ApiOperation("新增素材分组")
    @SysLog("素材分组")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody MaterialGroup materialGroup) {
        return R.okByRow(materialGroupService.save(materialGroup) ? 1 : 0);
    }

    /**
     * 修改素材分组
     */
    @ApiOperation("修改素材分组")
    @SysLog("素材分组")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody MaterialGroup materialGroup) {
        return R.okByRow(materialGroupService.updateById(materialGroup) ? 1 : 0);
    }

    /**
     * 删除素材分组
     */
    @ApiOperation("删除素材分组")
    @SysLog("素材分组")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")@PathVariable String[] ids) {
        return R.okByRow(materialGroupService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
